package com.jic.point.service.impl;

import com.jic.common.base.vo.PageResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.exception.AppRuntimeException;
import com.jic.point.mapper.ConfActivityRuleDetailTemplateMapper;
import com.jic.point.model.entity.ConfActivityRuleDetailTemplate;
import com.jic.point.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.rmi.server.ExportException;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import com.jic.point.model.entity.ConfActivityRuleDetailTemplateApply;
import com.jic.point.mapper.ConfActivityRuleDetailTemplateApplyMapper;
import com.jic.point.service.ConfActivityRuleDetailTemplateApplyService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

/**
 * 接口实现类
 *
 * @author : admin
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class ConfActivityRuleDetailTemplateApplyServiceImpl implements ConfActivityRuleDetailTemplateApplyService {

	@Resource
	private ConfActivityRuleDetailTemplateApplyMapper applyMapper;
	@Resource
	private ConfActivityRuleDetailTemplateMapper mapper;

	/**
	 * 新增记录
	 *
	 * @param request 需要保存的入参实体类
	 * @return int 是否保存成功
	 */
	@Override
	public int save(ConfActivityRuleDetailTemplateApply request) {
	 int flag = applyMapper.insert(request);
	 if (flag == 1) {
	   return 1;
	 }else {
	   throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
	 }
	}


	/**
	 * 保存或修改记录信息
	 * @param request 需要保存的入参实体类
	 * @return int 是否保存或修改成功
	 * author admin
	 * @date 2020-3-11 20:54:09
	 */
	@Override
	public int saveOrUpdate(ConfActivityRuleDetailTemplateApply request){
		log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
		try {
			if (request == null) {
				throw new AppRuntimeException(ResponseCode.EXCEPTION,"入参为空");
			}
			if (request.getId() != null) {
				log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
				int flag = applyMapper.updateByPrimaryKey(request);
				if(flag == 1){
				  return 1;
				}
			}else{
				log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
				request.setApplierId(request.getAdminOperate().getOperator());//申请人id
				request.setCreateTime(new Date());//创建时间
				request.setCreator(request.getAdminOperate().getOperator());
				request.setCreatorName(request.getAdminOperate().getOperateName());
				request.setCurrentAction(0);
				request.setCode("TEMPLATE"+ DateUtils.format(new Date(),"yyyyMMddHHmmssSSS"));
				log.info("保存或修改记录信息，保存，入参============================ = {}", JSONObject.toJSONString(request));
				int flag = applyMapper.insertSelective(request);
				if(flag == 1){
				  return 1;
				}else{
				  throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改失败");
				}
			}
		} catch (Exception e) {
			log.error("用户保存或修改失败", e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION,"用户保存或修改失败");
		}
	     return -1;
	}

	/**
	 * 删除记录
	 *
	 * @param request 需要删除,含有主键的入参实体类
	 * @return int 是否删除成功
	 */
	@Override
	public int deleteByPrimaryKey(ConfActivityRuleDetailTemplateApply request) {
		int flag = applyMapper.deleteByPrimaryKey(request);
	    if (flag == 1) {
	      return 1;
	    }else {
	      throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
	    }
	}


	/**
	 * 逻辑删除记录
	 *
	 * @param request 含有主键的逻辑删除实体类
	 * @return int 是否逻辑删除成功
	 */
	@Override
	public int deleteByPrimaryKeyLogically(ConfActivityRuleDetailTemplateApply request) {
		request.setDeleteFlag(1);
		int flag = applyMapper.updateByPrimaryKey(request);
	    if (flag == 1) {
	      return 1;
	    }else {
	      throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
	    }
	}


	/**
	 * 修改
	 *
	 * @param request
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(ConfActivityRuleDetailTemplateApply request){
		try {
			log.info("修改记录");
			ConfActivityRuleDetailTemplateApply param = new ConfActivityRuleDetailTemplateApply();
			BeanUtils.copyProperties(request, param);
			int flag = applyMapper.updateByPrimaryKeySelective(param);
	        if (flag == 1) {
	         return 1;
	        }else {
	          throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
	        }
		} catch (Exception e) {
			log.error("修改记录失败", e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改记录失败");
		}

	}

	/**
	 * 记录详情
	 *
	 * @param request 查询的入参实体类
	 * @return 数据库查询到的实体类
	 */
	@Override
	public ConfActivityRuleDetailTemplateApply getByPrimaryKey(ConfActivityRuleDetailTemplateApply request) {
		return applyMapper.selectByPrimaryKey(request);
	}


	/**
	 * 分页查询
	 *
	 * @param request 分页查询入参实体类
	 * @return 含有分页信息的实体类列表
	 */
	@Override
	public PageInfo<ConfActivityRuleDetailTemplateApply> listPageBySelective(ConfActivityRuleDetailTemplateApply request, int pageNum, int pageSize) {
	PageHelper.startPage(pageNum, pageSize);
	PageHelper.orderBy(" create_time desc");
	List<ConfActivityRuleDetailTemplateApply> list = applyMapper.listPageBySelective(request);
	PageInfo pageInfo = new PageInfo(list);
	PageResult<ConfActivityRuleDetailTemplateApply> result = new PageResult<>();
	result.setTotal(pageInfo.getTotal());
	result.setRows(list);
	return new PageInfo(list);
	}

	/**
	 * 不分页查询
	 *
	 * @param request 不分页查询入参实体类
	 * @return 数据库查到的实体类列表
	 */
	@Override
	public List<ConfActivityRuleDetailTemplateApply> listAllRecord(ConfActivityRuleDetailTemplateApply request) {
		//分页处理
		request.setDeleteFlag(0);
		return applyMapper.selectBySelective(request);
	}
/**
 * 审核啊  current_action  0-正常,1-修改,2-删除,
 */
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public int updateAudit(ConfActivityRuleDetailTemplateApply request) {
		log.info("活动规则模板============================审核入参{}",request);
		if(request.getAuditSts() == 2){ //审核失败
			// 修改 / 删除 审核失败  主表逻辑删除取消，申请表逻辑删除。
			int i = deleteByPrimaryKeyLogically(request); //申请表逻辑删除
			//主表根据 code逻辑删除
			ConfActivityRuleDetailTemplate param = new ConfActivityRuleDetailTemplate();
			param.setCode(request.getCode());
			param.setDeleteFlag(0);
			param.setAuditReason(request.getAuditReason());
			int i1 = mapper.deleteFlagTemplate(param);
			return i>0 || i1>0?1:0;
		}else if(request.getAuditSts() == 1){//审核成功
			if(request.getCurrentAction() == 1){ //判断修改
				//修改  审核成功
				// 修改主表数据
				ConfActivityRuleDetailTemplate param = new ConfActivityRuleDetailTemplate();
				BeanUtils.copyProperties(request,param);
				int i1 = mapper.updateByCode(param);//根据code修改
				//申请表逻辑删除
				request.setDeleteFlag(1);
				int i = applyMapper.updateByPrimaryKeySelective(request);
				return i>0 & i1>0?1:0;
			}else if (request.getCurrentAction() == 2){//判断删除
			//删除 审核成功 申请表逻辑删除
				int ii = deleteByPrimaryKeyLogically(request);
				return ii;
			}else{
				//新增审核通过
				ConfActivityRuleDetailTemplate param = new ConfActivityRuleDetailTemplate();
				BeanUtils.copyProperties(request,param);
				param.setId(null);
				param.setAuditorId(request.getAdminOperate().getOperator());
				int i = mapper.insertSelective(param);
				int i1 = deleteByPrimaryKeyLogically(request);
				return  i>0 & i1>0?1:0;
			}
		}else{//审核驳回
			request.setAuditSts(3);//设置驳回
			return updateByPrimaryKeySelective(request);
		}
	}


}
